package front;

import java.sql.Timestamp;
import be.db_backend;
import mt.middle;

public class Post{

	private int postID;
	private int threadID;
	private int userID;
	private String content;
	private Timestamp createdOn;
	private Timestamp lasteditedOn;
	private User createdBy;
	private String editContent;
	private middle m;
	
	public Post(){
		
	}
	public Post(int postID, int threadID, int userID, String content, Timestamp createdOn, Timestamp lasteditedOn){
		this.postID = postID;
		this.threadID = threadID;
		this.userID = userID;
		this.content = content;
		this.createdOn = createdOn;
		this.lasteditedOn = lasteditedOn;
		this.m = new middle(new db_backend());
		getUser();
	}
	
	public Post(int threadId, int createdByUserid, String content2) {
		this.postID = 0;
		this.threadID = threadId;
		this.userID = createdByUserid;
		this.content = content2;
		this.createdOn = new Timestamp(System.currentTimeMillis());
		this.lasteditedOn = new Timestamp(System.currentTimeMillis());
		this.m = new middle(new db_backend());
		getUser();
	}

	public synchronized void Create(){
		try {
			int id = m.CreatePost(postID, threadID, userID, content, createdOn, lasteditedOn);
			this.postID = id;
		} catch (Exception e) {
			System.out.println("FAIL! INSERT!");
			e.printStackTrace();
		}
	}
	
	public void editContent(String content){
		this.content = content;
		m.setPostContent(postID, content);
	}
	
	public void Edit(){
		
		
	}
	
	public void Delete(){
		m.DeletePost(postID);
	}
	
	public String toString(){
		return "postID: " + postID + 
				", threadID: " + threadID + 
				", userID: "  + userID + 
				", content: "  + content + 
				", createdOn: "  + createdOn + 
				", lasteditedOn: " + lasteditedOn;
	}
	
	public int getPostID() {
		return postID;
	}
	
	public void setPostID(int postID) {
		this.postID = postID;
	}
	
	public int getThreadID() {
		return threadID;
	}
	
	public void setThreadID(int threadID) {
		this.threadID = threadID;
	}
	
	public int getUserID() {
		return userID;
	}
	
	public void setUserID(int userID) {
		this.userID = userID;
	}
	
	public Timestamp getCreatedOn() {
		return createdOn;
	}
	
	public void setCreatedOn(Timestamp createdOn) {
		this.createdOn = createdOn;
	}
	
	public String getContent() {
		return content.replace("\n","<br>");
	}
	
	public void setContent(String content) {
		this.content = content;
	}
	public Timestamp getLasteditedOn() {
		return lasteditedOn;
	}
	
	public void setLasteditedOn(Timestamp lasteditedOn) {
		this.lasteditedOn = lasteditedOn;
	}

	public User getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(User createdBy) {
		this.createdBy = createdBy;
	}
	
	private void getUser(){
		try{
 			this.createdBy = m.getUser(userID);
 		}
		catch(Exception e){
			System.out.println("SQL statement error!");
			e.printStackTrace();
		}
	}
	public String getUserNameOfCreator(){
		if(createdBy == null)
			return "UNKNOWN USER";
		return createdBy.getEmail();
	}
	
	public String getEditContent() {
		editContent = content;
		return editContent;
	}
	public void setEditContent(String editContent) {
		this.editContent = editContent;
		this.content = editContent;
	}
}
